package com.shangs.platform.modular.cms.site.controller;

import com.shangs.platform.common.exception.BizExceptionEnum;
import com.shangs.platform.common.exception.BussinessException;
import com.shangs.platform.common.persistence.dao.CmsSiteMapper;
import com.shangs.platform.common.persistence.model.CmsSite;
import com.shangs.platform.core.base.controller.BaseController;
import com.shangs.platform.core.log.LogObjectHolder;
import com.shangs.platform.core.shiro.ShiroKit;
import com.shangs.platform.core.support.BeanKit;
import com.shangs.platform.core.util.ToolUtil;
import com.shangs.platform.modular.cms.warpper.SiteWarpper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 站点控制器
 *
 * @author shangs
 * @Date 2017-10-25 15:21:54
 */
@Controller
@RequestMapping("/site")
public class SiteController extends BaseController {

    private String PREFIX = "/cms/site/";

    @Resource
    CmsSiteMapper cmsSiteMapper;

    /**
     * 跳转到站点首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "site.html";
    }

    /**
     * 跳转到添加站点
     */
    @RequestMapping("/site_add")
    public String siteAdd() {
        return PREFIX + "site_add.html";
    }

    /**
     * 跳转到修改站点
     */
    @RequestMapping("/site_update/{siteId}")
    public String siteUpdate(@PathVariable Long siteId, Model model) {
        if (ToolUtil.isEmpty(siteId)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        CmsSite cmsSite = this.cmsSiteMapper.selectByPrimaryKey(siteId);
        Map<String, Object> map = BeanKit.beanToMap(cmsSite);
        model.addAttribute("site", map);
        LogObjectHolder.me().set(map);
        return PREFIX + "site_edit.html";
    }

    /**
     * 获取站点列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        List<Map<String, Object>> list = this.cmsSiteMapper.selectByCondition(new CmsSite());
        return new SiteWarpper(list).warp();
    }

    /**
     * 新增站点
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(CmsSite cmsSite) {
        cmsSite.setCreateId(ShiroKit.getUser().getId());
        cmsSite.setCreateTime(new Date());
        this.cmsSiteMapper.insertSelective(cmsSite);
        return SUCCESS_TIP;
    }

    /**
     * 删除站点
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Long siteId) {
        this.cmsSiteMapper.deleteByPrimaryKey(siteId);
        return SUCCESS_TIP;
    }


    /**
     * 修改站点
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(@Valid CmsSite cmsSite, BindingResult result) {
        if (result.hasErrors()) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        cmsSite.setUpdateId(ShiroKit.getUser().getId());
        cmsSite.setUpdateTime(new Date());
        this.cmsSiteMapper.updateByPrimaryKeySelective(cmsSite);
        return SUCCESS_TIP;
    }

    /**
     * 站点详情
     */
    @RequestMapping(value = "/detail")
    @ResponseBody
    public Object detail(@RequestParam Long siteId) {
        if (ToolUtil.isEmpty(siteId)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        CmsSite cmsSite = this.cmsSiteMapper.selectByPrimaryKey(siteId);
        Map<String, Object> map = BeanKit.beanToMap(cmsSite);
        return map;
    }
}
